package com.will.server.controller;


import com.google.gson.Gson;
import com.will.server.util.Base64Util;
import com.will.server.util.FileUtil;
import com.will.server.util.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLEncoder;
import java.util.Map;

@RestController
public class Test {
    /**
     * Auther sun
     * DATE 2023/1/20 17:09
     * VERSION 1.0
     */

    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("/test")
    public String test() {
        redisTemplate.opsForValue().set("name", "dongfang");
        return "ok";
    }

    @GetMapping("/test2")
    public String test2(@RequestBody String json) {
        String url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general";
        try {
            // 本地文件路径
            String filePath = "C:\\Users\\79312\\Desktop\\a.jpg";
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

            String param = "image=" + imgParam;

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = "24.7ab7fd6ff787b4be8c7e6e0c19a70fdb.2592000.1676986903.282335-29887558";

            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
